<?php
namespace wof;

class Letter {
    public $a_PlayerData = array();
    public $i_ConnId;

    // matrix = array (
                    // "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
                    // "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
                    // "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
                    // "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55",
                  // );
    
    public function __construct($a_PlayerData, $i_ConnId) {
        $this->a_PlayerData = $a_PlayerData;
        $this->i_ConnId = $i_ConnId;
    }

    /* Generate letter positions matrix */
    public function a_fGenerateLetterMatrix () {
        $a_Words = explode(" ", $this->a_PlayerData["quiz-answer"]);
        $a_Matrix = array();
        $sz_Matrix = "";
        $i_AnswerLength = strlen($this->a_PlayerData["quiz-answer"]);
        if ($i_AnswerLength <= 14) {
            $inc = 20;
            $des = 21;
        }
        else {
            if ($i_AnswerLength <= 28) {
                $inc = 34;
                $des = 35;
                $i_AnswerLength = $i_AnswerLength - 14;
                $a_Matrix = array (
                                    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"
                                  );
            }
            else {
                if ($i_AnswerLength <= 40) {
                    $inc = 48;
                    $des = 49;
                    $i_AnswerLength = $i_AnswerLength - 28;
                    $a_Matrix = array (
                                    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
                                    "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41"
                                  );
                }
                else {
                    $inc = 7;
                    $des = 8;
                    $i_AnswerLength = $i_AnswerLength - 40;
                    $a_Matrix = array (
                                    "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
                                    "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41",
                                    "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54"
                                  );
                }
            }
        }
        
        for ($i=0;$i<$i_AnswerLength;$i++) {
            if ( ($i % 2) == 0 ) {
                $a_Matrix[] = $des - 1;
                $des--;
            }
            else {
                $a_Matrix[] = $inc + 1;
                $inc++;
            }
        }

        sort($a_Matrix);
        for ($i=0;$i<strlen($this->a_PlayerData["quiz-answer"]);$i++) {
            if (substr($this->a_PlayerData["quiz-answer"],$i,1) == " ") {
                unset($a_Matrix[$i]);
            }
        }
        // Set to global
        $this->a_PlayerData["a_LetterMatrix"] = $a_Matrix;
        $sz_Matrix = implode(",",$a_Matrix);
        return $sz_Matrix;
    }
    
    /* Pick a letter in letter list */
    public function i_fPickup ($sz_Letter) {
        $a_Ret = array();
        $a_PosInMatrix = array();
        $a_LetterMatrix = $this->a_PlayerData["a_LetterMatrix"];
        $sz_Answer = str_replace(" ","",strtoupper($this->a_PlayerData["quiz-answer"]));
        $i_PreviousPosition = 0;
        $i_CountLetter = substr_count($sz_Answer,$sz_Letter);
        if ($i_CountLetter) {
            // Find position in answer string, then get in position matrix
            for ($i=0;$i<$i_CountLetter;$i++) {
                $i_Pos = stripos($sz_Answer,$sz_Letter,$i_PreviousPosition);
                $a_PosInMatrix[] = $a_LetterMatrix[$i_Pos];
                // Remove in Matrix
                array_splice($this->a_PlayerData["a_LetterMatrix"], $i_Pos - $i, 1);
                $i_PreviousPosition = $i_Pos + 1;
            }
            // Set score
            for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                if ($this->a_PlayerData['a_Player'][$i]['connId'] == $this->a_PlayerData['play_turn']) {
                    if ($this->a_PlayerData['temp_score']) {
                        $this->a_PlayerData['a_Player'][$i]['score'] = $this->a_PlayerData['a_Player'][$i]['score'] + $this->a_PlayerData['temp_score'];
                    }
                    else {
                        $this->a_PlayerData['freespin']++;
                    }
                    break;
                }
            }
            // Remove in Answer
            $this->a_PlayerData["quiz-answer"] = str_replace($sz_Letter,"",strtoupper($sz_Answer));
            // Check is last letter, end round
            if (strlen($this->a_PlayerData["quiz-answer"]) < 1) {
                $this->a_PlayerData['next_action'] = "endround";
                for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                    if ($this->a_PlayerData['a_Player'][$i]['connId'] != $this->a_PlayerData['play_turn']) {
                        $this->a_PlayerData['a_Player'][$i]['score'] = 0;
                    }
                }
            }
            else {
                $this->a_PlayerData['next_action'] = "spin";
            }
            $a_Ret['control'] = 'letter';
            $a_Ret['value'] = $sz_Letter . "%%S%%" . implode(",",$a_PosInMatrix);
            return $a_Ret;
        }
        else {
            // Set turn to next user
            if ($this->a_PlayerData['freespin'] && (int)$this->a_PlayerData['freespin'] > 0) {
                $this->a_PlayerData['next_action'] = "spin";
                $this->a_PlayerData['freespin']--;
            }
            else {
                $a_Ret['control'] = 'letter';
                $a_Ret['value'] = $sz_Letter . "%%S%%" . implode(",",$a_PosInMatrix);
                $this->a_PlayerData['next_action'] = "turn";
                return $a_Ret;
            }
        }
    }
    
    /* Select a letter in answer cells */
    public function i_fSelect ($i_Position) {
        $a_PosInMatrix = array();
        $a_LetterMatrix = $this->a_PlayerData["a_LetterMatrix"];
        $sz_Answer = str_replace(" ","",strtoupper($this->a_PlayerData["quiz-answer"]));
        $i_LetterPosition = 0;
        /* Get a letter position */
        for ($i=0;$i<count($a_LetterMatrix);$i++) {
            if ($a_LetterMatrix[$i] == $i_Position) {
                $i_LetterPosition = $i;
                // Remove in matrix
//                array_splice($this->a_PlayerData["a_LetterMatrix"], $i_Pos, 1);
                array_splice($this->a_PlayerData["a_LetterMatrix"], $i_LetterPosition, 1);
                break;
            }
        }
        // Remove in answer
        $this->a_PlayerData["quiz-answer"] = substr($sz_Answer, 0, $i_LetterPosition) . substr($sz_Answer, $i_LetterPosition + 1);
        // Check is last letter, end round
        if (strlen($this->a_PlayerData["quiz-answer"]) < 1) {
            $this->a_PlayerData['next_action'] = "endround";
            for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
                if ($this->a_PlayerData['a_Player'][$i]['connId'] != $this->a_PlayerData['play_turn']) {
                    $this->a_PlayerData['a_Player'][$i]['score'] = 0;
                }
            }
        }
        else {
            $this->a_PlayerData['next_action'] = "spin";
        }
        /* Select in answer string based on a position */
        $c_Letter = $i_Position . "%%S%%" . substr($sz_Answer,$i_LetterPosition,1);
        return $c_Letter;
    }
    
    /* Set turn to next player */
    public function loseturn () {
        $a_Ret = array();
        for ($i=0;$i<count($this->a_PlayerData['a_Player']);$i++) {
            if ((int)$this->a_PlayerData['a_Player'][$i]['connId'] == (int)$this->a_PlayerData['play_turn']) {
                /* Last player in player list */
                if ($i == (count($this->a_PlayerData['a_Player']) -1)) {
                    $this->a_PlayerData['play_turn'] = $this->a_PlayerData['a_Player'][0]['connId'];
                    $this->a_PlayerData['play_turn_name'] = $this->a_PlayerData['a_Player'][0]['username'];
                }
                else {
                    $this->a_PlayerData['play_turn'] = $this->a_PlayerData['a_Player'][$i+1]['connId'];
                    $this->a_PlayerData['play_turn_name'] = $this->a_PlayerData['a_Player'][$i+1]['username'];
                }
                break;
            }
        }
        $this->a_PlayerData['next_action'] = "spin";
        // Ativate turn
        $a_Ret['control'] = 'turn';
        $a_Ret['value'] = $this->a_PlayerData['play_turn_name'];
        return $a_Ret;
    }
}

?>